package com.baosight.sczx.zm.cc.service; 
import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemPenalty; 
/**
 * 生产问题处理---罚款记录
 */
public class ServiceZMCC14 extends ServiceEPBase {
	/**
	 * 初始化 
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo); 
		return outInfo;
	}
	/**
	 * 查询
	 */
	public EiInfo query(EiInfo inInfo)
	{
		String fdPenaltyIds = inInfo.getString("fdPenaltyIds");
		EiInfo outInfo=inInfo;
		if(fdPenaltyIds!=null&&!fdPenaltyIds.toString().trim().equals(""))
		{
			String _temp[] = fdPenaltyIds.split(",");
			StringBuffer fdKeyIds = new StringBuffer();
			for(int i = 0; i < _temp.length; i++){
				if(i==0) {
					fdKeyIds.append("'"+_temp[i]+"'");
				}
				else {
					fdKeyIds.append(",'"+_temp[i]+"'");
				}
			}
			StaticTools.setQueryParameters(inInfo, "fdKeyIds", fdKeyIds, true);//添加查询条件 
			outInfo = super.query(inInfo, "ZMCCProduceproblemPenalty.query", new ZMCCProduceproblemPenalty());
		}else
		{
			outInfo.addBlock("result").setBlockMeta(new ZMCCProduceproblemPenalty().eiMetadata);
		} 

//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));//部门
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));//部门
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType"));
		
		return outInfo;
	}
	/**
	 * 新增方法 
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCCProduceproblemPenalty _dom = new ZMCCProduceproblemPenalty();
		EiBlock eiBlock = inInfo.getBlock("penalty_add");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
	
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM_PENALTY"));
			_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));
		    this.dao.insert("ZMCCProduceproblemPenalty.insert", _dom);
		    
			//改主表状态 已罚款，并且追加罚款主键ＩＤ，罚款总额
			Map<String, String> params1 = new HashMap<String, String>();
			params1.put("fdKeyId", _dom.getFdRecordId()); 
			params1.put("fdPenaltyIds", _dom.getFdKeyId()); 
			this.dao.update("ZMCCProduceproblem.modifyPenalty", params1);
			//更新处理表中的数据
			if(_dom.getFdHandleId()!=null&&!_dom.getFdHandleId().trim().equals(""))
			{
				params1 = new HashMap<String, String>();
				params1.put("fdKeyId", _dom.getFdHandleId());
				params1.put("fdIsPenalty", "是");
				params1.put("fdPenaltyId", _dom.getFdKeyId());
				params1.put("fdPenalty", _dom.getFdPenalty().toString()); 
				this.dao.update("ZMCCProduceproblemHandle.updatePenalty", params1);
			} else {
			
				//直接罚款，不处理时，改变主表状态为“关闭”		
				Map<String, String> params2 = new HashMap<String, String>();
				params2.put("fdKeyId", _dom.getFdRecordId());

				this.dao.update("ZMCCProduceproblem.close", params2);
			}
		}
		out = this.query(inInfo); 
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
	} 
}
